package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.PermissionDao;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.dao.UserDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.User;
import com.itheima.health.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zhangmeng
 * @description 用户服务实现类
 * @date 2019/9/6
 **/
@Slf4j
@DubboService
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

    @Override
    public User findByUsername(String username) {
        //先查基本信息
        User user = userDao.selectByUsername(username);
        if (null == user) {
            //用户不存在
            return null;
        }
        //根据用户ID查角色信息
        List<Role> roles = roleDao.selectByUserId(user.getId());
        for (Role role : roles) {
            //根据角色ID查权限信息
            List<Permission> permissions = permissionDao.selectByRoleId(role.getId());
            role.getPermissions().addAll(permissions);
        }
        user.getRoles().addAll(roles);
        return user;
    }
    @Transactional
    @Override
    public void deleteByUserId(Integer userId) {
        userDao.deleteRoleIdsByUserId(userId);
        userDao.deleteByUserId(userId);
    }

    /**
     * @Author: Administrator
     * @Date: 14:47 2021/1/19
     * @Parms [user, roleIds]
     * @ReturnType: void
     * @Description: 编辑用户信息
    */
    @Transactional
    @Override
    public void edit(User user, Integer[] roleIds) {
        //更新用户信息
        userDao.updateById(user);
        //把用户关联的角色id删除
        userDao.deleteRoleIdsByUserId(user.getId());
        //重新绑定新的角色id
        for (Integer roleId : roleIds) {
            userDao.insertUserAndRole(user.getId(),roleId);
        }
    }
    /**
     * @Author: Administrator
     * @Date: 14:33 2021/1/19
     * @Parms [userId]
     * @ReturnType: java.util.List<java.lang.Integer>
     * @Description: 根据用户id查询角色id
    */
    @Override
    public List<Integer> findRoleIdsByUserId(String userId) {
        List<Integer>list=userDao.findRoleIdsByUserId(userId);
        return list;
    }

    /**
     * @Author: Administrator
     * @Date: 11:06 2021/1/19
     * @Parms [user, roleIds]
     * @ReturnType: boolean
     * @Description: 新增用户
    */
    @Transactional
    @Override
    public boolean add(User user, Integer[] roleIds) {
        //根据用户名查询用户
        User selectByUsername = userDao.selectByUsername(user.getUsername());
        //如果用户不存在 可以添加
        if (null==selectByUsername){
            userDao.insert(user);
            for (Integer roleId : roleIds) {
                userDao.insertUserAndRole(user.getId(),roleId);
            }
            return true;
        }else {
            //用户已存在,添加失败 返回false
            return false;
        }
    }

    /**
     * @Author: Administrator
     * @Date: 9:53 2021/1/19
     * @Parms [queryPageBean]
     * @ReturnType: java.util.List<com.itheima.health.pojo.User>
     * @Description: 分页查询用户信息
    */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<User> page=userDao.selectByCondition(queryPageBean.getQueryString());
        //封装返回值
        return new PageResult(page.getTotal(),page.getResult());
    }
    /**
     * @Author: Administrator
     * @Date: 12:00 2021/1/19
     * @Parms [id]
     * @ReturnType: com.itheima.health.pojo.User
     * @Description: 根据用户id查询用户
    */
    @Override
    public User findById(Integer id) {
        return userDao.selectById(id);
    }
}
